import { AllowNull, BelongsTo, Column, ForeignKey, Model, Table } from 'sequelize-typescript';

// 页面信息表
@Table({
  tableName: 'lcode_project',
})
export class Project extends Model<Project> {
  @Column({
    primaryKey: true,
    autoIncrement: true,
  })
  id: string;

  @AllowNull
  @Column({ field: 'name' })
  name: string;

  @AllowNull
  @Column({ field: 'platform' })
  platform: string;

  @AllowNull
  @Column({ field: 'desc' })
  desc: string;

  @AllowNull
  @Column({ field: 'status' })
  status: number;

  @AllowNull
  @Column({ field: 'basis' })
  basis: string;

  @AllowNull
  @Column({ field: 'configs' })
  configs: string;

  @AllowNull
  @Column({ field: 'created_user' })
  createdUser: string;

  @AllowNull
  @Column({ field: 'updated_user' })
  updatedUser: string;

  @AllowNull
  @Column({ field: 'created_at' })
  createdAt: string;

  @AllowNull
  @Column({ field: 'updated_at' })
  updatedAt: string;

  public static findAndCountAllPage(opts: any = {}) {
    const curr = opts.curr || 1;
    const size = opts.size || 10;
    opts.limit = size; // 每页查询的条数,对应pageSize
    opts.offset = (curr - 1) * size; // 从X条开始查询,对应page
    return this.findAndCountAll(opts).then((res) => {
      const { count, rows } = res;
      return {
        list: rows, // 当前页查询到的数据
        page: {
          curr, // 当前页
          size, // 每页条数
          totalPage: Math.ceil(count / size), // 总页数
          totalCount: count, // 总条数
        },
      };
    });
  }
}
